package com.zm.demo.core.leetcode;

import java.util.Stack;

/**
 * @author zoum
 * @create 2020/1/7 10:54
 */
public class Kuohao {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++){
            if (stack.size() == 0){
                stack.push(s.charAt(i));
            }else {
                Character before = stack.peek();
                Character current = s.charAt(i);
                if (mapping(before, current)){
                    stack.pop();
                }else {
                    stack.push(s.charAt(i));
                }
            }
        }

        return stack.size()>0;
    }

    private boolean mapping(Character c1, Character c2){
        if (c1 == null || c2 == null){
            return false;
        }
        if (c1 == '{' && c2 == '}'){
            return true;
        }
        if (c1 == '[' && c2 == ']'){
            return true;
        }
        if (c1 == '(' && c2 == ')'){
            return true;
        }
        return  false;
    }


    public static void main(String[] args){
        new Kuohao().isValid("()");
    }
}
